//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;

namespace OpenLS.Spreadsheet.Expressions
{
    abstract class BinaryExpressionBase<TExpression>: Expression where TExpression: Expression
    {
        protected BinaryExpressionBase(TExpression left, TExpression right)
        {
            _left = left;
            _right = right;
        }
        public sealed override IEnumerable<Expression> GetSubExpressions()
        {
            yield return Left;
            yield return Right;
        }
        public override sealed Expression Offset(CellRelativeAddress a)
        {
            return CreateExpression((TExpression)Left.Offset(a), (TExpression)Right.Offset(a));
        }

        protected abstract Expression CreateExpression(TExpression left, TExpression right);
        private readonly TExpression _left;
        public TExpression Left
        {
            get { return _left; }
        }
        private readonly TExpression _right;


        public TExpression Right
        {
            get { return _right; }
        }
        public override IEnumerable<IDependencyNode> GetPrecedents(EvaluationContext context)
        {
            foreach (IDependencyNode c in Left.GetPrecedents(context))
                yield return c;
            foreach (IDependencyNode c in Right.GetPrecedents(context))
                yield return c;
        }
    }
}
